<?php
include("./config.php");
include("./utils.php");
?>
<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <title>登录账号</title>
  <?php include 'scripts-inc.php' ?>
  <script src="./js/jquery.md5.js"></script>

  <style>
  .form-group{
    margin:8px;
  }
  .btn-right{
    float:right;
    margin-right:15px;
    width:88px;
  }
  .btn-center{
    float:auto;
    width:128px;
  }
  </style>
  <script>
  function template_login(){/*
{name}({id})
*/}

  $(function(){
      //get_page(5012111);

      //var userid = $.cookie("userid");
    var userid = "";
      
    $("#search-container").hide();
    $("#change-theme").hide();
    $("#register").click(function(){
      var data = {
        op:"reg",
        name:$("#reg-account-name").val(),
        mail:$("#reg-account-mail").val(),
        passwd:$("#reg-account-passwd").val(),
      };

      if (data.name.length < 2){
        show_msg("#msg", "昵称太短了！");
        return;
      }
      var reg=/^([a-zA-Z0-9]+[_|\-|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\-|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/gi;
      if(!reg.test(data.mail)) {
        show_msg("#msg", "请填写正确的邮箱");
        return;
      }

      if (data.passwd.length < 6){
        show_msg("#msg", "密码应该大于6位");
        return;
      }

      if (data.passwd != $("#account-repeat-passwd").val()){
        show_msg("#msg", "两次输入的密码不一致！");
        return;
      }
      
      data.passwd = $.md5($.md5(data.passwd));
      $.post("account-data.php", data, function(msg){
        if(msg == 0){
          show_msg("#msg", "添加失败");
          return;
        }
        show_msg("#msg", "添加成功：ID=" + msg);
        eval('msg =' + msg);
        //$.cookie("userid", msg.id);
        //$.cookie("name", msg.name);
      });
    });

    $("#login").click(function(){
      var data = {
        op:"log",
        mail:$("#login-account-mail").val(),
        passwd:$("#login-account-passwd").val(),
      };

      if (data.mail.length < 2){
        show_msg("#msg", "邮箱/ID太短了！");
        return;
      }

      if (data.passwd.length < 6){
        show_msg("#msg", "密码不对！");
        return;
      }

      data.passwd = $.md5($.md5(data.passwd));

      //$.cookie("userid",data.mail);
      $.post("account-data.php", data, function(msg){
        var s = msg;
        if(msg == 0 || msg == "0"){
          show_msg("#msg", "账号/密码不正确");
          return;
        }
          //show_msg("#msg", "登录成功: " + msg);
        eval('msg =' + msg);
        //$.cookie("userid", msg.id);
        //$.cookie("name", msg.name);
        if (msg.id){
          $("#account-container").hide();
          $("#login-success").show();
          var s = hereTemplate(template_login, msg);
          $("#login-userid").html(s);
        } else {
          show_msg("#msg", "登录失败：" + s);
        }
        
      });
    });

    $("#goto-login").click(function(){
      $("#account-login").show();
      $("#account-signup").hide();
    });
    $("#goto-signup").click(function(){
      $("#account-signup").show();
      $("#account-login").hide();
    });
  });//end of $(function(){

  </script>
</head>
<body>
<!-- template -->
<!-- end of template -->


<div id="wrapper">
<div id="container">
<!-- .................................................... -->

<div id="nav_data_src">
    <?php include 'navbars-inc.php' ?>
</div>

<div id="account-container" class="col-sm-offset-2 col-sm-6">
<div id="account-signup" style="display:none">
  <div class="form-group row">
    <span class="col-sm-5 lead"><strong>注册首页账号</strong></span>
    <a href="#" class="btn btn-warning btn-right" id="goto-login">登录&gt;&gt;</a>
  </div>
  <div class="form-group row">
    <label for="account-name" class="col-sm-2 control-label">昵称</label>
    <div class="col-sm-10">
      <input type="" class="form-control" id="reg-account-name" name="account-name" placeholder="昵称">
    </div>
  </div>
  <div class="clearfix"></div>
  <div class="form-group row">
    <label for="account-mail" class="col-sm-2 control-label">邮箱</label>
    <div class="col-sm-10">
      <input type="" class="form-control" id="reg-account-mail" name="account-mail" placeholder="邮箱">
    </div>
  </div>
  <div class="clearfix"></div>
  <div class="form-group row">
    <label for="login-account-passwd" class="col-sm-2 control-label">密码</label>
    <div class="col-sm-10">
      <input type="password" class="form-control" id="reg-account-passwd" name="login-account-passwd" placeholder="密码">
    </div>
  </div>
  <div class="clearfix"></div>
  <div class="form-group row">
    <label for="account-repeat-passwd" class="col-sm-2 control-label">重复密码</label>
    <div class="col-sm-10">
      <input type="password" class="form-control" id="reg-account-repeat-passwd" name="account-repeat-passwd" placeholder="重复密码">
    </div>
  </div>
  <div class="clearfix"></div>
  <div class="form-group row">
    <div class="col-sm-12 text-center">
      <a class="btn btn-lg btn-success btn-center" id="register">注册</a>
    </div>
  </div>
</div>

<form id="account-login" action="account-data.php" method="post">
  <div class="form-group row">
    <span class="col-sm-5 lead"><strong>登录账号</strong></span>
    <a href="#" class="btn btn-warning btn-right" id="goto-signup">注册&gt;&gt;</a>
  </div>

  <input type="" class="hidden" id="op" name="op" value="log"/>
  <?php
    if(!empty($_GET['ref_url'])){
      $ref_url = $_GET['ref_url']; 
      echo '<input type="" class="hidden" id="req-url" name="req-url" value="'.$ref_url.'"/>';
    }
  ?>

  <div class="form-group row">
    <label for="login-account-mail" class="col-sm-2 control-label">邮箱/ID</label>
    <div class="col-sm-10">
      <input type="" class="form-control" id="login-account-mail" name="login-account-mail" placeholder="邮箱/ID"/>
    </div>
  </div>
  <div class="clearfix"></div>
  <div class="form-group row">
    <label for="login-account-passwd" class="col-sm-2 control-label">密码</label>
    <div class="col-sm-10">
      <input type="password" class="form-control" id="login-account-passwd" name="login-account-passwd" placeholder="密码"/>
    </div>
  </div>
  <div class="clearfix"></div>
  <div class="form-group row">
    <div class="col-sm-12 text-center">
      <a  class="btn btn-lg btn-primary btn-center" id="login">登录</a>
    </div>
  </div>
  <div id="msg">
  </div>
</form>

</div> <!-- end of account-container -->

<div class="clearfix"></div>

<div class="clearfix"></div>
    
<div id="content" class="center-block">
    <div id="login-success" class="center-block text-center col-xs-12 col-sm-8 col-md-8 col-lg-8" style="display:none;">
      亲爱的<span id="login-userid" class="text-danger"></span>，欢迎您！<br/>
      <a href="index.php" class="btn btn-lg btn-success btn-center">前往首页</a>
    </div>
</div><!-- end of content -->

<!-- .................................................... -->
</div><!-- end of container -->
</div><!-- end of wrapper -->
</body>
</html>
